﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace LED_Tester
{
    class ClassExportExcel
    {
        private DataGridView dgv;
        private string[] info;
        private int[] count;
        private string path;

        public void Process()
        {
            // 建立Excel对象
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook myWorkBook = excel.Application.Workbooks.Add(true);
            excel.Visible = false;
            // 通过headNo确定输出的列标题和列内容
            ArrayList headArray = new ArrayList();
            for (int i = 0; i < count.Length; i++)
            {
                if (count[i] != 0)
                    headArray.Add(count[i] * (i + 1));
            }
            int[] headNum = new int[headArray.Count];
            for (int i = 0; i < headArray.Count; i++)
            {
                headNum[i] = int.Parse(headArray[i].ToString());
            }
            // 写基本信息
            excel.Cells[1, 1] = "产品类型：";
            excel.Cells[1, 2] = info[0];
            excel.Cells[2, 1] = "厂    商：";
            excel.Cells[2, 2] = info[1];
            excel.Cells[3, 1] = "日    期：";
            excel.Cells[3, 2] = info[2];
            excel.Cells[4, 1] = "环境温度：";
            excel.Cells[4, 2] = info[3] + "℃";
            excel.Cells[5, 1] = "环境湿度：";
            excel.Cells[5, 2] = info[4] + "%";
            excel.Cells[6, 1] = "操 作 者：";
            excel.Cells[6, 2] = info[5];;
            // 生成字段名称
            excel.Cells[7, 1] = dgv.Columns[0].HeaderText;
            for (int i = 0, j = 2; i < headNum.Length; i++, j++)
            {
                excel.Cells[7, j] = dgv.Columns[headNum[i]].HeaderText;
            }
            // 填充数据
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                excel.Cells[i + 10, 1] = dgv[0, i].Value.ToString();

            }
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                for (int j = 0; j < headNum.Length; j++)
                {
                    if (dgv[headNum[j], i].Value != null)
                        excel.Cells[i + 10, j + 2] = dgv[headNum[j], i].Value.ToString();

                }
            }
            object missing = System.Reflection.Missing.Value;
            excel.ActiveWorkbook.Saved = true;
            excel.ActiveWorkbook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, missing, missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
            myWorkBook.Close(missing, missing, missing);
            myWorkBook = null;
            excel.Quit();
        }

        public void StartThread(DataGridView dgvData, string[] testInfo, int[] dataCount, string excelPath)
        {
            dgv = dgvData;
            info = testInfo;
            count = dataCount;
            path = excelPath;

            Thread thread = new Thread(Process);
            thread.Start();
        }
    }
}
